package com.inspur.edp.cef.designtime.api.validate.element;

import com.alibaba.druid.util.StringUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.validate.common.CheckUtil;
import com.inspur.edp.cef.designtime.api.variable.CommonVariable;

/**
 * The Definition Of DimensionInfo
 *
 * @ClassName: DimensionInfo
 * @Author: Benjamin Gong
 * @Date: 2021/1/11 17:13
 * @Version: V1.0
 */
public abstract class FieldChecker {
    protected boolean isLegality(String code) {
        return CheckUtil.isLegality(code);
    }

    public final void checkField(IGspCommonField field, IGspCommonDataType commonDataType) {
        checkBaseFieldInfo(field, commonDataType);
        checkFieldWithObjectType(field, commonDataType);
        checkFieldExtension(field);
    }

    protected void checkFieldExtension(IGspCommonField field) {

    }

    private void checkFieldWithObjectType(IGspCommonField field, IGspCommonDataType commonDataType) {
        if (field == null) return;
        switch (field.getObjectType()) {
            case Association:
                AssociationFieldChecker.getInstance().checkAssociationField(field, commonDataType);
                break;
            case Enum:
                EunmFieldChecker.getInstance().checkEunm(field, commonDataType);
                break;
            case None:
                break;
            case DynamicProp:
                break;
            default:
                break;
        }
    }

    private void checkBaseFieldInfo(IGspCommonField field, IGspCommonDataType gspCommonDataType) {
        if (field instanceof CommonVariable) {
            if(StringUtils.isEmpty(field.getLabelID())){
                if (field.getName() == null || field.getName().isEmpty()) {
                    CheckUtil.exception("变量的字段编号不允许为空,请修改！");
                }
                CheckUtil.exception("变量的字段["+field.getName()+"]编号不允许为空,请修改！");
            }
            if (!isLegality(field.getLabelID()))
                CheckUtil.exception("变量的字段编号[" +field.getLabelID()+"]是Java关键字,请修改！");
        } else {
            if(StringUtils.isEmpty(field.getLabelID())){
                if (field.getName() == null || field.getName().isEmpty()) {
                    CheckUtil.exception("节点[" + gspCommonDataType.getCode() + "]的字段编号不允许为空,请修改！");
                }
                CheckUtil.exception("节点[" + gspCommonDataType.getCode() + "]的字段["+field.getName()+"]编号不允许为空,请修改！");
            }
            if (!isLegality(field.getLabelID()))
                CheckUtil.exception("节点[" + gspCommonDataType.getCode() + "]的字段编号[" +field.getLabelID()+"]是Java关键字,请修改！");
        }

        checkBaseExtension();
    }

    protected void checkBaseExtension() {

    }
}
